'use strict';
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');

module.exports = class extends Generator {
  prompting() {
    // 欢迎信息
    this.log(
      yosay(`欢迎使用 ${chalk.red('Hello微服务')} 模块生成器!`)
    );

    const prompts = [
      {
        type: 'input',
        name: 'moduleName',
        message: '请输入模块名称 (例如: hello-demo):',
        default: 'hello-demo'
      },
      {
        type: 'input',
        name: 'moduleDescription',
        message: '请输入模块描述:',
        default: '新的Hello微服务模块'
      },
      {
        type: 'input',
        name: 'basePackage',
        message: '请输入基础包名:',
        default: 'com.hello'
      },
      {
        type: 'list',
        name: 'moduleType',
        message: '请选择模块类型:',
        choices: [
          {
            name: '业务模块 (包含Controller/Service/Mapper)',
            value: 'business'
          },
          {
            name: '通用模块 (仅基础结构)',
            value: 'common'
          }
        ],
        default: 'business'
      },
      {
        type: 'confirm',
        name: 'includeRedis',
        message: '是否需要Redis支持?',
        default: false
      }
    ];

    return this.prompt(prompts).then(props => {
      // 保存用户输入的配置
      this.props = props;
    });
  }

  writing() {
    // 生成项目目录结构
    this._createProjectStructure();
    
    // 生成Maven配置文件
    this._createMavenConfig();
    
    // 生成Java源码文件
    this._createJavaFiles();
    
    // 生成配置文件
    this._createConfigFiles();
  }

  _createProjectStructure() {
    const moduleName = this.props.moduleName;
    
    // 创建目录结构
    this.fs.copy(
      this.templatePath('gitignore'),
      this.destinationPath(`${moduleName}/.gitignore`)
    );
    
    // 创建src目录结构
    this.fs.copy(
      this.templatePath('src/main/resources/banner.txt'),
      this.destinationPath(`${moduleName}/src/main/resources/banner.txt`)
    );
    
    // 创建部署目录
    this.fs.copy(
      this.templatePath('deploy/Dockerfile'),
      this.destinationPath(`${moduleName}/deploy/Dockerfile`)
    );
  }

  _createMavenConfig() {
    const moduleName = this.props.moduleName;
    const basePackage = this.props.basePackage;
    
    // 生成pom.xml
    this.fs.copyTpl(
      this.templatePath('pom.xml'),
      this.destinationPath(`${moduleName}/pom.xml`),
      {
        moduleName: moduleName,
        moduleDescription: this.props.moduleDescription,
        basePackage: basePackage,
        includeRedis: this.props.includeRedis
      }
    );
  }

  _createJavaFiles() {
    const moduleName = this.props.moduleName;
    const basePackage = this.props.basePackage;
    const pkgPath = basePackage.replace(/\./g, '/');
    const moduleNameWithoutPrefix = moduleName.replace(/^hello-/, '');
    
    // 生成类名 - 将模块名转换为驼峰格式并添加Application后缀
    const className = this._generateClassName(moduleNameWithoutPrefix);
    
    // 创建主应用类
    this.fs.copyTpl(
      this.templatePath('src/main/java/Application.java'),
      this.destinationPath(`${moduleName}/src/main/java/${pkgPath}/${moduleNameWithoutPrefix}/${className}.java`),
      {
        basePackage: basePackage,
        moduleNameWithoutPrefix: moduleNameWithoutPrefix,
        className: className
      }
    );
    
    // 如果是业务模块，则创建Controller/Service/Mapper
    if (this.props.moduleType === 'business') {
      // 创建Controller
      this.fs.copyTpl(
        this.templatePath('src/main/java/controller/Controller.java'),
        this.destinationPath(`${moduleName}/src/main/java/${pkgPath}/${moduleNameWithoutPrefix}/controller/DemoController.java`),
        {
          basePackage: basePackage,
          moduleNameWithoutPrefix: moduleNameWithoutPrefix
        }
      );
      
      // 创建Service
      this.fs.copyTpl(
        this.templatePath('src/main/java/service/Service.java'),
        this.destinationPath(`${moduleName}/src/main/java/${pkgPath}/${moduleNameWithoutPrefix}/service/DemoService.java`),
        {
          basePackage: basePackage,
          moduleNameWithoutPrefix: moduleNameWithoutPrefix
        }
      );
      
      // 创建Mapper
      this.fs.copyTpl(
        this.templatePath('src/main/java/mapper/Mapper.java'),
        this.destinationPath(`${moduleName}/src/main/java/${pkgPath}/${moduleNameWithoutPrefix}/mapper/DemoMapper.java`),
        {
          basePackage: basePackage,
          moduleNameWithoutPrefix: moduleNameWithoutPrefix
        }
      );
    }
  }

  // 生成类名的方法 - 将模块名转换为驼峰式并添加Application后缀
  _generateClassName(moduleNameWithoutPrefix) {
    // 将模块名按连字符或下划线分割
    const parts = moduleNameWithoutPrefix.split(/[-_]/);
    
    // 将每部分首字母大写
    const camelCaseName = parts.map(part => 
      part.charAt(0).toUpperCase() + part.slice(1)
    ).join('');
    
    // 返回类名
    return `Hello${camelCaseName}Application`;
  }

  _createConfigFiles() {
    const moduleName = this.props.moduleName;
    const basePackage = this.props.basePackage;
    const moduleNameWithoutPrefix = moduleName.replace(/^hello-/, '');
    
    // 创建application.yml
    this.fs.copyTpl(
      this.templatePath('src/main/resources/application.yml'),
      this.destinationPath(`${moduleName}/src/main/resources/application.yml`),
      {
        moduleName: moduleName,
        basePackage: basePackage,
        moduleNameWithoutPrefix: moduleNameWithoutPrefix
      }
    );
    
    // 创建bootstrap.yml
    this.fs.copyTpl(
      this.templatePath('src/main/resources/bootstrap.yml'),
      this.destinationPath(`${moduleName}/src/main/resources/bootstrap.yml`),
      {
        moduleName: moduleName
      }
    );
  }

  install() {
    this.log(chalk.green('项目结构已生成完成!'));
  }

  end() {
    this.log('');
    this.log(chalk.green('Hello微服务模块已创建成功!'));
    this.log('');
    this.log(`可以通过执行以下命令添加到主项目：`);
    this.log(chalk.yellow(`1. 将 ${this.props.moduleName} 目录移动到项目根目录`));
    this.log(chalk.yellow(`2. 在根 pom.xml 中添加该模块引用`));
    this.log('');
  }
}; 